package com.ia.bikerecife.client;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

public class ClientResponderThread extends Thread {

    private String       mMessage = "";

    private final Socket socket;

    public ClientResponderThread(Socket socket) {
        this.socket = socket;
    }

    public void run() {
        try {
            DataOutputStream out = new DataOutputStream(socket.getOutputStream());
            DataInputStream in = new DataInputStream(socket.getInputStream());

            readInputMessage(in);
            returnResponse(out);

            out.close();
            in.close();
            socket.close();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void readInputMessage(DataInputStream in) throws Exception {
        mMessage = in.readUTF();
        System.out.println("Entrada: " + mMessage);
    }

    public void returnResponse(DataOutputStream out) throws IOException {

        ClientResponder p = new ClientResponder();
        mMessage = p.ask(mMessage);
        System.out.println("Saida: " + mMessage);
        out.writeUTF(mMessage);
    }

}